package com.ctshk.rpc.custom.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.vo.MainDataCount;
import com.ctshk.rpc.custom.dto.trip.TripScheduleTouristCountDTO;
import com.ctshk.rpc.custom.entity.CustomizedTripScheduleTouristCount;
import com.ctshk.rpc.custom.mapper.CustomizedTripScheduleTouristCountMapper;
import com.ctshk.rpc.custom.req.trip.TripScheduleTouristCountReq;
import com.ctshk.rpc.custom.service.ICustomizedTripScheduleTouristCountService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 团期旅客类型统计 服务实现类
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-03-12
 */
@DubboService
public class CustomizedTripScheduleTouristCountServiceImpl extends ServiceImpl<CustomizedTripScheduleTouristCountMapper, CustomizedTripScheduleTouristCount> implements ICustomizedTripScheduleTouristCountService {

    @Autowired
    private CustomizedTripScheduleTouristCountMapper tripScheduleTouristCountMapper;

    /**
     * 根据团期id获取旅客统计
     *
     * @param scheduleId
     * @return
     */
    @Override
    public List<TripScheduleTouristCountDTO> queryBySchedule(Long scheduleId) {
        QueryWrapper<CustomizedTripScheduleTouristCount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schedule_id",scheduleId);
        queryWrapper.eq("is_deleted",0);
        List<CustomizedTripScheduleTouristCount> list = tripScheduleTouristCountMapper.selectList(queryWrapper);
        if (list != null){
            List<TripScheduleTouristCountDTO> dtoList = EntityUtil.copyList(list, TripScheduleTouristCountDTO.class);
            return dtoList;
        }
        return null;
    }

    /**
     * 更新统计
     *
     * @param countReqList
     * @return
     */
    @Override
    public boolean batchUpdate(List<TripScheduleTouristCountReq> countReqList) {
        List<CustomizedTripScheduleTouristCount> list = EntityUtil.copyList(countReqList, CustomizedTripScheduleTouristCount.class);
        boolean bl = updateBatchById(list);
        return bl;
    }

    /**
     * 团期旅客类型统计回滚
     *
     * @param list
     * @param scheduleId
     * @param userId
     * @return
     */
    @Override
    public boolean rollBackCount(List<MainDataCount> list, Long scheduleId, Long userId) {
        QueryWrapper<CustomizedTripScheduleTouristCount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schedule_id",scheduleId);
        queryWrapper.eq("is_deleted",0);
        List<CustomizedTripScheduleTouristCount> countList = tripScheduleTouristCountMapper.selectList(queryWrapper);
        if (countList != null && countList.size() > 0){
            for (CustomizedTripScheduleTouristCount touristCount : countList) {
                for (MainDataCount mainDataCount : list) {
                    if (touristCount.getTouristTypeId().longValue() == mainDataCount.getMainDataId().longValue()){
                        touristCount.setOrderPeople(touristCount.getOrderPeople() - mainDataCount.getCountNumber().intValue());
                        touristCount.setModifiedId(userId);
                    }
                }
            }
            return updateBatchById(countList);
        }
        return true;
    }
}
